package com.ai.bitcoin.config;

import com.ai.bitcoin.wechat.handler.*;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static me.chanjar.weixin.common.api.WxConsts.*;

@Configuration
public class WechatConfiguration {

    /**
     * 设置微信公众号的appid
     */
    @Value("${app.id}")
    private String appId;

    /**
     * 设置微信公众号的app secret
     */
    @Value("${app.secret}")
    private String secret;

    /**
     * 设置微信公众号的token
     */
    @Value("${app.token}")
    private String token;

    /**
     * 设置微信公众号的EncodingAESKey
     */
    @Value("${app.aesKey}")
    private String aesKey;

    @Autowired
    private LogHandler logHandler;
    @Autowired
    private QrCodeScanHandler qrCodeScanHandler;
    @Autowired
    private MenuViewHandler menuViewHandler;
    @Autowired
    private LocationHandler locationHandler;
    @Autowired
    private MenuClickHandler menuClickHandler;
    @Autowired
    private MsgHandler msgHandler;
    @Autowired
    private UnsubscribeHandler unsubscribeHandler;
    @Autowired
    private SubscribeHandler subscribeHandler;

    @Bean
    WxMpMessageRouter wechatMessageRouter() {
        final WxMpMessageRouter wechatMessageRouter = new WxMpMessageRouter(wechatService());

        // 记录所有事件的日志 （异步执行）
        wechatMessageRouter.rule().handler(this.logHandler).next();

        //自定义菜单事件
        wechatMessageRouter.rule().async(false).msgType(XmlMsgType.EVENT)
                .event(MenuButtonType.CLICK).handler(this.menuClickHandler).end();

        // 点击菜单连接事件
        wechatMessageRouter.rule().async(false).msgType(XmlMsgType.EVENT)
                .event(MenuButtonType.VIEW).handler(this.menuViewHandler).end();

        // 关注事件
        wechatMessageRouter.rule().async(false).msgType(XmlMsgType.EVENT)
                .event(EventType.SUBSCRIBE).handler(this.subscribeHandler)
                .end();

        // 取消关注事件
        wechatMessageRouter.rule().async(false).msgType(XmlMsgType.EVENT)
                .event(EventType.UNSUBSCRIBE)
                .handler(this.unsubscribeHandler).end();

        // 上报地理位置事件
        wechatMessageRouter.rule().async(false).msgType(XmlMsgType.EVENT)
                .event(EventType.LOCATION).handler(this.locationHandler)
                .end();

        // 接收地理位置消息
        wechatMessageRouter.rule().async(false).msgType(XmlMsgType.LOCATION)
                .handler(this.locationHandler).end();

        // 扫码事件
        wechatMessageRouter.rule().async(false).msgType(XmlMsgType.EVENT)
                .event(EventType.SCAN).handler(this.qrCodeScanHandler).end();

        // 默认
        wechatMessageRouter.rule().async(false).handler(this.msgHandler).end();

        return wechatMessageRouter;
    }

    @Bean
    public WxMpService wechatService() {
        WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
        configStorage.setAppId(appId);
        configStorage.setSecret(secret);
        configStorage.setToken(token);
        configStorage.setAesKey(aesKey);
        WxMpService service = new WxMpServiceImpl();
        service.setWxMpConfigStorage(configStorage);
        return service;
    }


}
